#!/bin/bash

centos_install () {
  # 安装软件源
  sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

  # 安装数据库
  sudo yum install -y postgresql13-server

  # 初始化数据库
  sudo /usr/pgsql-13/bin/postgresql-13-setup initdb

  # 添加postgresql到系统启动服务中。
  sudo systemctl enable postgresql-13

  # 启动服务
  sudo systemctl start postgresql-13

}

ubuntu_install () {
  # 添加软件源
  sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

  # 导入软件源验证密钥
  wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -

  # 获取软件包更新和最新信息
  sudo apt update

  # 安装数据库
  sudo apt -y install postgresql
}

OSNAME="ubuntu"

CHECK_NAME=`cat /etc/os-release | egrep '^NAME=.*CentOS|^NAME=.*RedHat'`

if [ -n "$CHECK_NAME" ] ; then
    OSNAME="centos"
fi

if [ $OSNAME = "ubuntu" ] ; then
    ubuntu_install
else
    centos_install
fi
